package com.cs.store.controller;

import com.cs.common.result.HttpResp;
import com.cs.common.result.ResultCode;
import com.cs.domain.entity.ShopCar;
import com.cs.domain.vo.StoreGoodsVo;
import com.cs.store.service.IShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Objects;

@RestController
@RequestMapping("/api/shopCar")
public class ShopCarController {
    @Autowired
    private IShopCarService iShopCarService;


    /**
     *商品加入到购物车
     * @param storeGoodsId 店铺商品id
     * @param buyNum    购买数量
     * @param
     * @return
     */
    @GetMapping("addShopCar")
    public HttpResp addShopCar(Long storeGoodsId,String username,Long buyNum) {
        if (Objects.isNull(buyNum)){
            return HttpResp.result(ResultCode.SHOPCAR_ADD_ERROR, new Date(),null);
        }else {
       //取到店铺商品id，查店铺商品表，得到商品的名字和单价
        StoreGoodsVo storeGoodsVo =  iShopCarService.findBystGoodsId(storeGoodsId);
        //创建购物车对象
        ShopCar shopCar = new ShopCar();
        //userid从前台取
        shopCar.setUserid(iShopCarService.finduserId(username));
        shopCar.setGoodsId(storeGoodsId);
        shopCar.setPrice(storeGoodsVo.getGoodsPrice());
        shopCar.setStoreId(storeGoodsVo.getStoreId());
        shopCar.setCount(buyNum);  //封装对象后执行添加购物车
        return HttpResp.result(ResultCode.SHOPCAR_ADD_SUCCESS, new Date(), iShopCarService.addShopCar(shopCar));}
    }

    @GetMapping("findCar")
    public HttpResp findCar(String username,Long storeId) {

//        iShopCarService.findCar(userId,storeId);
        return HttpResp.result(ResultCode.CAR_SHOW_SUCCESS, new Date(),  iShopCarService.findCar(username,storeId));
    }


    @GetMapping("deleCarGoods")
    public HttpResp deleCarGoods(Long carId) {
        return HttpResp.result(ResultCode.SHOP_CAR_DEL_SUCCESS, new Date(),  iShopCarService.deleCarGoods(carId));
    }







}
